class Solution {
public:
    bool check(TreeNode* root, TreeNode* subRoot)
    {
        if (!root && !subRoot)
        {
            return true;
        }
        if ((!root && subRoot) || (root && !subRoot) || (root->val != subRoot->val))
        {
            return false;
        }
        return check(root->left, subRoot->left) && check(root->right, subRoot->right);
    }

    bool dfs(TreeNode* root, TreeNode* subRoot)
    {
        if (!root) return false;
        return check(root, subRoot) || dfs(root->left, subRoot) || dfs(root->right, subRoot);
    }
    bool isSubtree(TreeNode* root, TreeNode* subRoot) {
        return dfs(root, subRoot);
    }
};